﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Framework.Verifications;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Protocol.equipment;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol.mission;
using Pixysoft.Weblications.Sango.Constant;

namespace Pixysoft.Weblications.Sango.Command.mission
{
    partial class SangoMissionHandler
    {
        public mission_dailysubmit mission_dailysubmit(string primarycode)
        {
            string userCode = SangoManager.Authentication();

            mission_dailysubmit response = ReflectionManager.CreatePojo<mission_dailysubmit>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);
            Entities.Usr_Mission activity = MissionDao.getMission(userCode);

            if (MissionFormular.isMissionDailyWeeklyRefresh(activity))
            {
                MissionDao.refreshDailyMissionWeekly(activity);
                SettingHelper.GetNoebe().EntityNoebeCommand.Update(activity);
                response.Message = "每日任务刷新了。";
                return response;
            }
            else if (MissionFormular.isMissionDailyDailyRefresh(activity))
            {
                MissionDao.refreshDailyMissionDaily(activity);
                SettingHelper.GetNoebe().EntityNoebeCommand.Update(activity);
                response.Message = "每日任务刷新了。";
                return response;
            }

            if (!MissionDao.commitDailyMission(activity, primarycode))
            {
                throw Exceptions.VerificationFailedException(primarycode);
            }

            LoggerHelper.Debug("finish commit daily mission:{0}", activity.Dailymissionlist);

            DailyMissionItem selectedItem = MissionDao.getDailyMissionItem(activity, primarycode);

            Entities.Eqm_Equipment eqm = MissionHelper.trySearchEquipment(selectedItem.MissionTypeCode, selectedItem.Level);

            Entities.Usr_Building building = UsrBuildingDao.getUserBuilding(userCode, CtiBuildingTypecodeCollection.ISI);

            IEntityNoebeTransaction command = SettingHelper.EntityTransaction;

            command.Begin(delegate()
            {
                if (eqm != null)
                {
                    Entities.Usr_Equipment usrEqm = EquipmentDao.tryDropUserEquipment(command, profile, eqm.Equipmentserialcode);
                    response.eqm = EquipmentFacade.ToEquipment(usrEqm, new List<Entities.Usr_Equipmentextra>());
                }

                long soulvalue = MissionFormular.getMissionDailySoulValue(
                    profile.Userlevel, selectedItem.MissionTypeCode, selectedItem.Level, building.Buildinglevel);

                UsrProfileDao.increaseSoulvalue(profile, soulvalue);

                command.Update(profile);

                command.Update(activity);

                command.Commit();

            });

            response.acceptlist = MissionFacade.ToAcceptMissions(profile, activity, building.Buildinglevel);
            response.newlist = MissionFacade.ToNewMissions(profile, activity, building.Buildinglevel);
            response.profile = UsrProfileFacade.ToProfile(profile);
            response.todaymax = MissionFormular.default_max_dailymission;
            response.todaydone = activity.Dailymissioncount;
            response.Succeed = true;
            return response;
        }


    }
    public interface mission_dailysubmit : ISangoResponse
    {
        player_profile profile { get;set;}

        long todaymax { get;set;}
        long todaydone { get;set;}

        List<mission_daily> acceptlist { get;set;}
        List<mission_daily> newlist { get;set;}

        usr_equipment eqm { get;set;}
    }
}
